import 'dart:convert';

import 'package:date_format/date_format.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_hotwork/bean/shenbaolistbean.dart';
import 'package:flutter_hotwork/home/RoadChoice.dart';
import 'package:flutter_hotwork/homepage/BaseTitleBar.dart';
import 'package:flutter_hotwork/homepage/BaseViewBar.dart';
import 'package:flutter_hotwork/utils/DioUtils.dart';
import 'package:flutter_hotwork/utils/LogUtil.dart';
import 'package:flutter_hotwork/xunjian/weight/table_widget.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:logger/logger.dart';

import '../Toast.dart';

class yongliangshenbaoscreen extends StatefulWidget {
  @override
  ChartPageState createState() => ChartPageState();
}

class SeriesDatas {
  final int time;
  final double data;

  SeriesDatas(this.time, this.data);
}

class ChartPageState extends State<yongliangshenbaoscreen> {
//手机号的控制器
  TextEditingController phoneController = TextEditingController();
  Map selectArea;
  ShenBaoListBean listbean;
  TabController _tabController;
  var time = formatDate(DateTime.now(), [yyyy, '-', mm, "-", dd]);
  var time2 = formatDate(DateTime.now(), [yyyy, '-', mm, "-", dd]);
  ScrollController controller = ScrollController();

  //列表所有数据
  List<List<String>> allList = [];

  @override
  Widget build(BuildContext context) {
    double rpx = MediaQuery.of(context).size.height / 800;
    double rpxw = MediaQuery.of(context).size.width / 750;

    Logger().d(allList);
    return MaterialApp(
      home: Scaffold(
          appBar: BaseViewBar(
              childView: BaseTitleBar(
                "用户用量申报",
                leftIcon: 'images/fanhui.png',
                rightIcon: "=",
                rightClick: () {
                  // Navigator.push(context, MaterialPageRoute(builder: (context) {
                  //   return  shenbaoactivity();
                  // }));
                },
                leftClick: () {
                  // deactivate();
                  Navigator.of(context).pop();
                  print("点击了左边干嘛啊。。。哦");
                },
              ),
              preferredSize: Size.fromHeight(50.0)),
          body: Column(
            children: [
              Container(
                child: Row(
                  children: [
                    Container(
                      margin:
                          EdgeInsets.only(left: 0, right: 0, top: 5, bottom: 5),
                      child: InkWell(
                        onTap: () {
                          showModalBottomSheet(
                              context: context,
                              builder: (_) => Container(
                                    height: 200.0,
                                    child: AreaSelection(
                                      onSelect: handleSelect,
                                      initProviceIndex: selectArea == null
                                          ? 0
                                          : selectArea['proviceIndex'],
                                      initCityIndex: selectArea == null
                                          ? 0
                                          : selectArea['cityIndex'],
                                    ),
                                  ));
                        },
                        child: Container(
                            height: 20,
                            margin: EdgeInsets.only(
                                left: 10, right: 0, top: 0, bottom: 0),
                            padding: EdgeInsets.only(
                                left: 2, right: 0, top: 0, bottom: 0),
                            decoration: BoxDecoration(
                              border: Border.all(
                                  color: Colors.blue, width: 0.5), // 边色与边宽度
                              color: Colors.white, // 底色
                              //        borderRadius:  BorderRadius.circular((20.0)), // 圆角度
                            ),
                            child: Row(
                              children: [
                                Container(
                                  width: 100,
                                  child: Text(
                                    selectArea != null
                                        ? selectArea['address']
                                        : "请选择用户",
                                    style: TextStyle(
                                        color: Colors.black, fontSize: 10),
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                ),
                                Container(
                                  width: 5,
                                ),
                                Image.asset(
                                  'images/jiantou.png',
                                  width: 14.0,
                                  height: 20.0,
                                ),
                              ],
                            )),
                      ),
                    ),
                    InkWell(
                      onTap: () {
                        getList();
                        // getmaxandminList();
                      },
                      child: Container(
                          width: 50,
                          height: 20,
                          margin: EdgeInsets.only(
                              left: 10, right: 0, top: 0, bottom: 0),
                          padding: EdgeInsets.only(
                              left: 2, right: 2, top: 0, bottom: 0),
                          decoration: BoxDecoration(
                            // border:  Border.all(color: Colors.blue, width: 0.5), // 边色与边宽度
                            color: Colors.blue, // 底色
                            borderRadius: BorderRadius.circular((5.0)), // 圆角度
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text(
                                "查询",
                                style: TextStyle(
                                    color: Colors.white, fontSize: 10),
                              )
                            ],
                          )),
                    ),
                  ],
                ),
              ),
              Container(
                height: 30,
                child: Row(
                  children: [
                    Text("   开始时间："),
                    FlatButton(
                      onPressed: () {
                        DatePicker.showDatePicker(context,
                            // 是否展示顶部操作按钮
                            showTitleActions: true,
                            // 最小时间
                            minTime: DateTime(2018, 3, 5),
                            // 最大时间
                            maxTime: DateTime(2099, 6, 7),
                            // change事件
                            onChanged: (date) {
                          print('change $date');
                        },
                            // 确定事件
                            onConfirm: (date) {
                          time = formatDate(date, [yyyy, '-', mm, "-", dd]);
                          setState(() {});
                        },
                            // 当前时间
                            currentTime: DateTime.now(),
                            // 语言
                            locale: LocaleType.zh);
                      },
                      child: Container(
                          width: 100,
                          height: 20,
                          margin: EdgeInsets.only(
                              left: 0, right: 0, top: 0, bottom: 0),
                          padding: EdgeInsets.only(
                              left: 10, right: 0, top: 0, bottom: 0),
                          decoration: BoxDecoration(
                            border: Border.all(color: Colors.blue, width: 0.5),
                            // 边色与边宽度
                            color: Colors.white, // 底色
                            //        borderRadius:  BorderRadius.circular((20.0)), // 圆角度
                          ),
                          child: Row(
                            children: [
                              Image.asset(
                                'images/rili.png',
                                width: 14.0,
                                height: 20.0,
                              ),
                              Container(
                                width: 5,
                              ),
                              Text(
                                '$time',
                                style: TextStyle(
                                    color: Colors.black, fontSize: 10),
                              )
                            ],
                          )),
                    ),
                  ],
                ),
              ),
              Container(
                height: 30,
                child: Row(
                  children: [
                    Text("   结束时间："),
                    FlatButton(
                      onPressed: () {
                        DatePicker.showDatePicker(context,
                            // 是否展示顶部操作按钮
                            showTitleActions: true,
                            // 最小时间
                            minTime: DateTime(2018, 3, 5),
                            // 最大时间
                            maxTime: DateTime(2099, 6, 7),
                            // change事件
                            onChanged: (date) {
                          print('change $date');
                        },
                            // 确定事件
                            onConfirm: (date) {
                          time2 = formatDate(date, [yyyy, '-', mm, "-", dd]);
                          setState(() {});
                          print('confirm $time2');
                        },
                            // 当前时间
                            currentTime: DateTime.now(),
                            // 语言
                            locale: LocaleType.zh);
                      },
                      child: Container(
                          width: 100,
                          height: 20,
                          margin: EdgeInsets.only(
                              left: 0, right: 0, top: 0, bottom: 0),
                          padding: EdgeInsets.only(
                              left: 10, right: 0, top: 0, bottom: 0),
                          decoration: BoxDecoration(
                            border: Border.all(color: Colors.blue, width: 0.5),
                            // 边色与边宽度
                            color: Colors.white, // 底色
                            //        borderRadius:  BorderRadius.circular((20.0)), // 圆角度
                          ),
                          child: Row(
                            children: [
                              Image.asset(
                                'images/rili.png',
                                width: 14.0,
                                height: 20.0,
                              ),
                              Container(
                                width: 5,
                              ),
                              Text(
                                '$time2',
                                style: TextStyle(
                                    color: Colors.black, fontSize: 10),
                              )
                            ],
                          )),
                    ),
                  ],
                ),
              ),
              Expanded(
                child: allList.length != 0
                    ? TableWidget(titleList, allList, onTap: (x, y) {
                        print("外部  x=$x y=$y");
                      })
                    : Center(child: Text("暂无数据")),
              ),
            ],
          )),
    );
  }

  Widget _generateFirstColumnRow(BuildContext context, int index) {
    if (index == 0) {
      return Container(
        width: 80,
        child: Text("日期"),
      );
    } else {
      return Container(
        width: 80,
        // child:Text("啦啦啦")
        child: Text(listbean.result[index - 1].createDay),
      );
    }
  }

  Widget _generateRightHandSideColumnRow(BuildContext context, int index) {
    if (index == 0) {
      return Row(children: [
        Container(
          width: 50,
          child: Text("0时"),
        ),
        Container(
          width: 50,
          child: Text("1时"),
        ),
        Container(
          width: 50,
          child: Text("2时"),
        ),
        Container(
          width: 50,
          child: Text("3时"),
        ),
        Container(
          width: 50,
          child: Text("4时"),
        ),
        Container(
          width: 50,
          child: Text("5时"),
        ),
        Container(
          width: 50,
          child: Text("6时"),
        ),
        Container(
          width: 50,
          child: Text("7时"),
        ),
        Container(
          width: 50,
          child: Text("8时"),
        ),
        Container(
          width: 50,
          child: Text("9时"),
        ),
        Container(
          width: 50,
          child: Text("10时"),
        ),
        Container(
          width: 50,
          child: Text("11时"),
        ),
        Container(
          width: 50,
          child: Text("12时"),
        ),
        Container(
          width: 50,
          child: Text("13时"),
        ),
        Container(
          width: 50,
          child: Text("14时"),
        ),
        Container(
          width: 50,
          child: Text("15时"),
        ),
        Container(
          width: 50,
          child: Text("16时"),
        ),
        Container(
          width: 50,
          child: Text("17时"),
        ),
        Container(
          width: 50,
          child: Text("18时"),
        ),
        Container(
          width: 50,
          child: Text("19时"),
        ),
        Container(
          width: 50,
          child: Text("20时"),
        ),
        Container(
          width: 50,
          child: Text("21时"),
        ),
        Container(
          width: 50,
          child: Text("22时"),
        ),
        Container(
          width: 50,
          child: Text("23时"),
        ),
        Container(
          width: 50,
          child: Text("合计"),
        ),
      ]);
    } else {
      return Row(children: [
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours0),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours1),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours2),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours3),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours4),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours5),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours6),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours7),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours8),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours9),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours10),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours11),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours12),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours13),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours14),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours15),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours16),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours17),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours18),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours19),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours20),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours21),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours22),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].hours23),
        ),
        Container(
          width: 50,
          child: Text(listbean.result[index - 1].sumDaily.toString()),
        ),
      ]);
    }
  }

  List<Widget> _getTitleWidget() {
    return [
      Text("日期"),
      Text("0 时"),
      Text("1 时"),
      Text("2 时"),
      Text("3 时"),
      Text("4 时"),
      Text("5 时"),
      Text("6 时"),
      Text("7 时"),
      Text("8 时"),
      Text("9 时"),
      Text("10时"),
      Text("11时"),
      Text("12时"),
      Text("13时"),
      Text("14时"),
      Text("15时"),
      Text("16时"),
      Text("17时"),
      Text("18时"),
      Text("19时"),
      Text("20时"),
      Text("21时"),
      Text("22时"),
      Text("23时"),
      Text("合计"),
    ];
  }

  List<String> titleList = [
    '日期',
    '0 时',
    '1 时',
    '2 时',
    '3 时',
    '4 时',
    '5 时',
    '6 时',
    '7 时',
    '8 时',
    '9 时',
    '10时',
    '11时',
    '12时',
    '13时',
    '14时',
    '15时',
    '16时',
    '17时',
    '18时',
    '19时',
    '20时',
    '21时',
    '22时',
    '23时',
    '合计'
  ];

//   void submit(Result result) async {
//     try {
//       try {
//         Response response;
//         response = await DioUtils.dio.post(DioUtils.API_PREFIX+"/declareSteam/updateDeclare", data: json.encode(result));
//
//         Map respMap = json.decode(response.data);
//         basebean base = basebean.fromJson(respMap);
//         if (base.code == 200) {
//           setState(() {
//
//           });
//           LocalToast.toast(context,msg: "提交成功");
//         } else {
//           LocalToast.toast(context,msg: base.message);
//         }
//         print('响应数据：' + response.toString());
//       } catch (e) {
//         //  LocalToast.toast(context,msg: "网络错误");
//         print('请求出错：' + e.toString());
//       }
//
//
//
//       print(phoneController.text);
// //      print(response);
//     } catch (e) {
//       print(e);
//     }
//   }

  ///接收选择的结果
  void handleSelect(Map targetArea) {
    setState(() {
      selectArea = targetArea;
      // initData(selectArea['proviceIndex'],selectArea['cityIndex']);
    });
  }

  @override
  void initState() {
    super.initState();
    DioUtils.createInstance();
  }

  @override
  void dispose() {
//    _amapLocation.stopLocate();
    super.dispose();
  }

  void getList() {
    try {
      DioUtils.dio
          .get(DioUtils.API_PREFIX +
              "/daily/consumption/listDailyConsumption?meterId=" +
              (selectArea != null ? selectArea['id'] : "") +
              "&&startDate=" +
              time +
              "&&endDate=" +
              time2)
          .then((value) {
        Map respMap = json.decode(value.data);
        listbean = ShenBaoListBean.fromJson(respMap);
        List listMap = respMap["result"];
        allList=[];
        if (listbean.code == 0) {
          if (listbean.result != null && listbean.result.length > 0) {
            for (var i = 0; i < listbean.result.length; i++) {
              List<String> contentList = [];
              contentList.add(""+listbean.result[i]?.createDay);
              contentList.add(""+listbean.result[i]?.hours0.toString());
              contentList.add(""+listbean.result[i]?.hours1.toString());
              contentList.add(""+listbean.result[i]?.hours2.toString());
              contentList.add(""+listbean.result[i]?.hours3.toString());
              contentList.add(""+listbean.result[i]?.hours4.toString());
              contentList.add(""+listbean.result[i]?.hours5.toString());
              contentList.add(""+listbean.result[i]?.hours6.toString());
              contentList.add(""+listbean.result[i]?.hours7.toString());
              contentList.add(""+listbean.result[i]?.hours8.toString());
              contentList.add(""+listbean.result[i]?.hours9.toString());
              contentList.add(""+listbean.result[i]?.hours10.toString());
              contentList.add(""+listbean.result[i]?.hours11.toString());
              contentList.add(""+listbean.result[i]?.hours12.toString());
              contentList.add(""+listbean.result[i]?.hours13.toString());
              contentList.add(""+listbean.result[i]?.hours14.toString());
              contentList.add(""+listbean.result[i]?.hours15.toString());
              contentList.add(""+listbean.result[i]?.hours16.toString());
              contentList.add(""+listbean.result[i]?.hours17.toString());
              contentList.add(""+listbean.result[i]?.hours18.toString());
              contentList.add(""+listbean.result[i]?.hours19.toString());
              contentList.add(""+listbean.result[i]?.hours20.toString());
              contentList.add(""+listbean.result[i]?.hours21.toString());
              contentList.add(""+listbean.result[i]?.hours22.toString());
              contentList.add(""+listbean.result[i]?.hours23.toString());
              contentList.add(""+listbean.result[i]?.sumDaily.toString());
              Logger().d(contentList);
              allList.add(contentList);
            }
            setState(() {});
          }
        } else {
          LocalToast.toast(context, msg: listbean.message);
        }
      });
    } catch (e) {
      LogUtil.d("请求出错----" + e.toString());
      LocalToast.toast(context, msg: "请选择线路");
    }
  }

//   genContentList(List<Result> data, BuildContext context, controller) {
//     double rpx = MediaQuery
//         .of(context)
//         .size
//         .width / 750;
//
//     return ListView.builder(
//       shrinkWrap: true,
//       controller: controller,
//       itemCount: data.length,
//       itemBuilder: (BuildContext context, int index) {
//         return
//           Card(
//             // padding: EdgeInsets.only(left: 10, right: 10, top: 0, bottom: 0),
//             child:
//             Row(
//               mainAxisAlignment: MainAxisAlignment.center,
//               mainAxisSize: MainAxisSize.max,
// //                交叉轴的布局方式，对于column来说就是水平方向的布局方式
//               crossAxisAlignment: CrossAxisAlignment.center,
//               //就是字child的垂直布局方向，向上还是向下
//               verticalDirection: VerticalDirection.down,
//               children: [
//                 Container(
//                     height: 30,
//                     width: rpx*115,
//                     child:
//                     Center(
//                       child:
//                       Text(data[index].createDay,
//                         style: TextStyle(color: Colors.black, fontSize: 8,),
//                       ),
//                     )
//                 ),
//                 // Container(
//                 //     height: 30,
//                 //     width: rpx*115,
//                 //     child:
//                 //     Center(
//                 //       child:
//                 //       // Text(data[index].flow.toString(),
//                 //       //   style: TextStyle(color: Colors.black, fontSize: 8,),
//                 //       ),
//                 //     )
//                 // ),
//
//
//
//
//               ],
//
//             ),
//
//           );
//         // Expanded(
//         //   child:
//         // Flexible(
//         //   child:
//         // getItemContentList(data[index].focusHotUserVos,context,controller),
//         // )
//         // )
//
//       },
//     );
//   }
}
